package com.mit.web.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class RemoveUploadedFileServlet
 */
public class RemoveUploadedFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(RemoveUploadedFileServlet.class);
	private final String PARAM_FILE_NAME = "filename";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RemoveUploadedFileServlet() {
        super();       
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String fileName = (String) request.getParameter(PARAM_FILE_NAME);
		String folder = this.getServletConfig().getServletContext().getRealPath("/") + UploadServlet.FOLDER_NAME;
		File file = new File(folder, fileName);
		logger.debug("Prepare to delete file: " + file.getPath());
		if(file.exists()){
			if(! file.delete())
				logger.error("Unable to delete the file.");
			else
				logger.error("File has been deleted.");
		}else{
			logger.error("File not exists: " + file.getPath());
		}
	}

}
